myApp.controller('ChartsCtrl', ['$scope','tools', function ($scope,tools) {
	
}]);
myApp.controller('ForgetCtrl', ['$scope','tools','$timeout', function ($scope,tools,$timeout) {
	if($(window).width()<=414){
		$scope.mobile = true;
	}else{
		$scope.mobile = false;
	}
	$('ul.tabs').tabs();
	$timeout(function(){
		$('ul.tabs').tabs('select_tab', 'phoneForget');
	},100);
	
}]);
myApp.controller('IndustryCtrl', ['$scope','tools','Session', function ($scope,tools,Session) {



	var title = Session.UserInfo.itemLevel1Category.split('--')[1];
	$scope.pieOption = {
		option:{
			title : {
				text: title,
				subtext: '',
				x:'center'
			},
			tooltip : {
				trigger: 'item',
				formatter: "{a} <br/>{b} : {c} ({d}%)"
			},
			series: [
			{
				name: '',
				type: 'pie',
				data:[
				{value:335, name:'直接访问'},
				{value:310, name:'邮件营销'},
				{value:234, name:'联盟广告'},
				{value:135, name:'视频广告'},
				{value:1548, name:'搜索引擎'}
				],
				itemStyle: {
					emphasis: {
						shadowBlur: 10,
						shadowOffsetX: 0,
						shadowColor: 'rgba(0, 0, 0, 0.5)'
					}
				}
			}
			]
		},
		post:{
			url:'',
			data:{}
		}
	}

	$scope.barOption = {
		option: {
			tooltip : {
				trigger: 'axis',
		        axisPointer : {            // 坐标轴指示器，坐标轴触发有效
		            type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
		        }
		    },
		    legend: {
		    	data:['直接访问','邮件营销','联盟广告','视频广告','搜索引擎','百度','谷歌','必应','其他']
		    },
		    grid: {
		    	left: '3%',
		    	right: '4%',
		    	bottom: '3%',
		    	containLabel: true
		    },
		    xAxis : [
		    {
		    	type : 'category',
		    	data : ['周一','周二','周三','周四','周五','周六','周日']
		    }
		    ],
		    yAxis : [
		    {
		    	type : 'value'
		    }
		    ],
		    series : [
		    {
		    	name:'直接访问',
		    	type:'bar',
		    	data:[320, 332, 301, 334, 390, 330, 320]
		    },
		    {
		    	name:'邮件营销',
		    	type:'bar',
		    	stack: '广告',
		    	data:[120, 132, 101, 134, 90, 230, 210]
		    },
		    {
		    	name:'联盟广告',
		    	type:'bar',
		    	stack: '广告',
		    	data:[220, 182, 191, 234, 290, 330, 310]
		    },
		    {
		    	name:'视频广告',
		    	type:'bar',
		    	stack: '广告',
		    	data:[150, 232, 201, 154, 190, 330, 410]
		    },
		    {
		    	name:'搜索引擎',
		    	type:'bar',
		    	data:[862, 1018, 964, 1026, 1679, 1600, 1570],
		    	markLine : {
		    		lineStyle: {
		    			normal: {
		    				type: 'dashed'
		    			}
		    		},
		    		data : [
		    		[{type : 'min'}, {type : 'max'}]
		    		]
		    	}
		    },
		    {
		    	name:'百度',
		    	type:'bar',
		    	barWidth : 5,
		    	stack: '搜索引擎',
		    	data:[620, 732, 701, 734, 1090, 1130, 1120]
		    },
		    {
		    	name:'谷歌',
		    	type:'bar',
		    	stack: '搜索引擎',
		    	data:[120, 132, 101, 134, 290, 230, 220]
		    },
		    {
		    	name:'必应',
		    	type:'bar',
		    	stack: '搜索引擎',
		    	data:[60, 72, 71, 74, 190, 130, 110]
		    },
		    {
		    	name:'其他',
		    	type:'bar',
		    	stack: '搜索引擎',
		    	data:[62, 82, 91, 84, 109, 110, 120]
		    }
		    ]
		},
		post:{
			url:'',
			data:{}
		}
	};

}]);
myApp.controller('LoginCtrl', ['$scope','tools','$cookies','$timeout','Session','$location','SweetAlert','NoticeServ','$http','$rootScope', function ($scope, tools,$cookies,$timeout,Session,$location,SweetAlert,NoticeServ,$http,$rootScope) {

	if($(window).width()<=414){
		$scope.mobile = true;
	}else{
		$scope.mobile = false;
	}
	$scope.User = {
		oldName:'',
		name:'',
		password:'',
		remember:false,
		rememberStuff:false,
		nameError:'',
		passwordError:'',
		init:function(){
			var data = $cookies.get('user');
			console.log(data);
			if(data){
				data=JSON.parse(data);
				this.name = data.name;
				this.password = data.password;
				this.remember = data.remember; 
				this.rememberStuff = true;
			}
		},
		isRemember:function(){			
			
			console.log(this.remember);
			if(this.remember){
				var data = {
					name:this.name,
					password:this.password,
					remember:true
				};

				$cookies.put('user',JSON.stringify(data));
				console.log($cookies.get('user'));
			}else{
				console.log('remove cookie');
				$cookies.remove('user');
			}
			
		},
		checkName:function(){
		
			console.log(this.name);
			console.log(this.oldName);
			if(this.name == '' || this.name == this.oldName){
				return;
			}
			
			var $this = this;
			tools.$post({
				url:'/users/queryByName',
				data:{
					name:this.name
				}
			}).then(function(resp){
				$this.oldName = $this.name;
				console.log(resp);
				if(resp.value.length == 0){
					$this.nameError = "该用户名不存在";
				}else{
					$this.nameError = "";
				}
				
			},function(errResp){

				console.log('error')
			});
		},
		login:function(){

			if(this.nameError !='' || this.name == '' || this.password == ''){
				return;
			}
			var $this = this;
			console.log(this.remember);
			tools.$post({
				url:'/users/login',
				// url:'proxy/marketconditions/user/userLogin.htm',
				data:{
					name:this.name,
					password:this.password
				}
			}).then(function(resp){
				console.log('++++')
				console.log(resp);
				$this.passwordError = "";
				$this.isRemember();
				Session.create(resp.value);
				$location.path('/share');
				NoticeServ.$broadcast('LOGIN_EV');
				// SweetAlert.swal({
				// 	title: "登录成功",
				// 	type: "success",
				// 	showCancelButton: false,
				// 	confirmButtonColor: "#DD6B55",
				// 	confirmButtonText: "去首页",
				// 	closeOnConfirm: true
				// },function(){ 
				// 	$timeout(function(){
				// 		$location.path('/share');
				// 		$rootScope.$broadcast('LOGIN_SUCCESS_EV');
				// 	},100);
				// });
			
			},function(errResp){
				console.log(errResp);


				if(errResp.message == "alreadyEnter"){
					SweetAlert.swal('','该用户已登录','error');
				}else{
					$this.passwordError = "密码错误";
					SweetAlert.swal('','账号或密码错误','error');
				}

				
				// console.log(errResp)
				console.log('error');
			});
		}
	};

	$scope.User.init();
}]);
directives.directive('usernameReg', function($q, $timeout,tools) {

    return {
        require: 'ngModel',
        link: function(scope, elm, attrs, ctrl) {
			// $(elm).bind('blur',function(ctrl){
				console.log('blur');

	            ctrl.$asyncValidators.username = function(modelValue, viewValue) {
	                console.log('modelValue:')
	                console.log(modelValue);
	                if (ctrl.$isEmpty(modelValue)) {
	                    // consider empty model valid
	                    return $q.resolve();
	                }
	                var def = $q.defer();
	                var value = modelValue;
					tools.$get({
						url:'/users/queryByName',
						data: {
							name:value
						}
					}).then(function(resp){
	                	console.log(resp);
	                	if(resp.value.length==0){
	                		console.log('0');
	                		def.reject();
	                	}else{
	                		console.log('1');
	                		def.resolve();
	                	}
	                	
	                },function(err){
	                	def.reject();
	                });
	                    
	                return def.promise;
	            };
	        // });  
        }  

    };
});
myApp.controller('RegCtrl', ['$scope','tools','SweetAlert','$location','$timeout', function ($scope, tools,SweetAlert,$location,$timeout) {


	if($(window).width()<=414){
		$scope.mobile = true;
	}else{
		$scope.mobile = false;
	}
	$('ul.tabs').tabs();
	$timeout(function(){
		$('ul.tabs').tabs('select_tab','phoneReg');
	},100);
	


	$scope.scond = 60;
	function setTime(){
        if($scope.scond!=0){
            $scope.scond--;
            $timeout(function(){                
                setTime();
            },1000);
        }else{
            $scope.User.getCodeStuff = false;
            $scope.scond = 60;
        }
    }
	// SweetAlert.swal('','注册失败','error');
	$scope.User = {
		oldName:'',
		name:'',
		password:'',
		password2:'',
		phone:'',
		email:'',
		nameError:'',
		passwordError:'',
		phoneError:'',
		emailError:'',
		getCodeStuff:false,
		getCode:function(){
			this.getCodeStuff = true;
			$timeout(function(){
				setTime();
			},1000);
		},
		checkName:function(){
			console.log(this.name);
			console.log(this.oldName);
			if(this.name == '' || this.name == this.oldName){
				return;
			}
			
			var $this = this;
			tools.$post({
				url:'/users/queryByName',
				data:{
					name:this.name
				}
			}).then(function(resp){
				$this.oldName = $this.name;
				console.log(resp);
				if(resp.value.length == 0){
					$this.nameError = "";
				}else{
					$this.nameError = "该用户名已存在";
				}
				
			},function(errResp){

				console.log('error')
			});
		},
		checkPassword:function(){
			if(this.password != this.password2){
				this.passwordError = '两次密码不一致';
				
			}else{
				this.passwordError = '';

			}
		},
		checkEmail:function(){
			var regExp = new RegExp(/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/);
			if(regExp.test(this.email)){
				this.emailError = '';
			}else{
				this.emailError = '邮箱格式错误';
			}
		},
		checkPhone:function(){
			var regExp = new RegExp(/^1[34578]\d{9}$/);
			if(regExp.test(this.phone)){
				this.phoneError = '';
			}else{
				this.phoneError = '手机格式错误';
			}
		},
		reg:function(){

			this.checkPassword();
			if(this.nameError !='' || this.passwordError != '' || this.emailError !='' || this.name=='' || this.password=='' || this.email ==''){
				SweetAlert.swal('','请填写正确的注册信息','warning');
				return;
			}
			tools.$post({
				url:'/users/insert',
				// url:'/proxy/marketconditions/user/userLogin.htm',
				data:{
					name: this.name,
					password: this.password,
					email: this.email
				}
			}).then(function(resp){
				console.log(resp);
				if(resp.value.insertId){
					SweetAlert.swal({
						title: "注册成功",
						type: "success",
						showCancelButton: false,
						confirmButtonColor: "#DD6B55",
						confirmButtonText: "去登录",
						closeOnConfirm: true
					},function(){ 
						$timeout(function(){
							$location.path('/login');
						},100);
					});
				}else{
					SweetAlert.swal('','注册失败','error');
				}
				
			},function(errResp){
				console.log('error');
			});
		}
	};


}]);
myApp.controller('SetCtrl', ['$scope','Session','FileUploader', function ($scope,Session,FileUploader) {
	console.log('SetCtrl loading');
	if($(window).width()<=414){
        $scope.mobile = true;
    }else{
        $scope.mobile = false;
    }

    $scope.User = Session.UserInfo;
    console.log($scope.User);


    $scope.openFile = function(){
    	$('#imgUpload').click();
    }

    var uploader = $scope.uploader = new FileUploader({
        url: '/users/logo'
    });
    $scope.fileupOk = function(){
        console.log('queue');
        console.log(uploader.queue);
        uploader.queue[0].upload();
    }
    uploader.onSuccessItem = function(fileItem, response, status, headers) {
        console.info('onSuccessItem', fileItem, response, status, headers);
    };
    uploader.onErrorItem = function(fileItem, response, status, headers) {
        console.info('onErrorItem', fileItem, response, status, headers);
    };      
}]);
myApp.controller('ShareCtrl', ['$scope', function ($scope) {
	$(document).ready(function(){
		$('.scrollspy').scrollSpy();
	});
	$('.dropdown-button').dropdown();
}]);